-- FUNCTION: public.widget_GetSampleRecievedCount()

-- DROP FUNCTION public."widget_GetSampleRecievedCount"();

CREATE OR REPLACE FUNCTION public."widget_GetSampleRecievedCount"(
	"fromDate" date DEFAULT NULL::date)
    RETURNS TABLE("Count" bigint) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
begin
return query
select count(*) from "LabBookingTimeLine" lbt
	join "LabBookingStatus" lbs on lbs."LabBookingStatusId" = lbt."LabBookingStatusId" where lbs."Status" = 'SampleRecieved'
	and lbt."CreatedDate" :: date ="fromDate";
end
$BODY$;

ALTER FUNCTION public."widget_GetSampleRecievedCount"(date)
    OWNER TO postgres;
===================================================================================
-- DROP FUNCTION public."widget_GetVerifiedCount"();

CREATE OR REPLACE FUNCTION public."widget_GetVerifiedCount"(
	"fromDate" date DEFAULT NULL::date)
    RETURNS TABLE("Count" bigint) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
begin
return query
select count(*) from "LabBookingTimeLine" lbt
	join "LabBookingStatus" lbs on lbs."LabBookingStatusId" = lbt."LabBookingStatusId" where lbs."Status" = 'Verified'
	and lbt."CreatedDate" :: date ="fromDate";
end
$BODY$;

ALTER FUNCTION public."widget_GetVerifiedCount"(date)
    OWNER TO postgres;
===========================================================================================
-- DROP FUNCTION public."widget_GetTransferCount"();

CREATE OR REPLACE FUNCTION public."widget_GetTransferCount"(
	"fromDate" date DEFAULT NULL::date)
    RETURNS TABLE("Count" bigint) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
begin
return query
select COUNT(*) from "LabTransferHeader" LT
	join "LabTransferDetail" LD on LD."LabTransferHeaderId"=LT."LabTransferHeaderId"
	join "Account" A on A."AccountId" = LT."TransferedBy"
	where LT."TransferedDate"::date="fromDate";
end
$BODY$;

ALTER FUNCTION public."widget_GetTransferCount"(date)
    OWNER TO postgres;
============================================================================================================
-- FUNCTION: public.widget_GetCanceledCount()

-- DROP FUNCTION public."widget_GetCanceledCount"();
CREATE OR REPLACE FUNCTION public."widget_GetCanceledCount"(
	"fromDate" date DEFAULT NULL::date,
      "referenceId" integer DEFAULT NULL::integer,
	"locationId" integer DEFAULT NULL::integer )
    RETURNS TABLE("Count" bigint) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
begin
return query

select COUNT(*)
 from "NewLabBookingHeader" LH
 join "NewLabBookingDetail" LD on LD."NewLabBookingHeaderId"=LH."NewLabBookingHeaderId"
  join "Provider" pr on pr."ProviderId"= LH."DoctorId"
	where LH."CreatedDate"::date="fromDate" and  LD."LabBookingStatusId"=2 ;
end
$BODY$;

ALTER FUNCTION public."widget_GetCanceledCount"(date,integer,integer)
    OWNER TO postgres;
==============================================================================================
-- FUNCTION: public.widget_GetSampleCount()

-- DROP FUNCTION public."widget_GetSampleCount"();
CREATE OR REPLACE FUNCTION public."widget_GetSampleCount"(
	"fromDate" date DEFAULT NULL::date,
      "referenceId" integer DEFAULT NULL::integer,
	"locationId" integer DEFAULT NULL::integer   )
    RETURNS TABLE("Count" bigint) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
begin
return query
select COUNT(*) from "LabSampleCollection" LS
 join "NewLabBookingDetail" LD on LD."NewLabBookingDetailId"=LS."NewLabBookingDetailId"
 join "Account" A on A."AccountId"=LS."SampleCollectedBy"
 where LS."CollectionDate"::date="fromDate"
 and case when "locationId" is null then 1=1 else LS."LocationId"= "locationId" end;
 end;
$BODY$;

ALTER FUNCTION public."widget_GetSampleCount"(date,int,int)
    OWNER TO postgres;